package cn.hzxs.zjn.studentmanagement.controller;

import cn.hzxs.zjn.studentmanagement.dao.UserMapper;
import cn.hzxs.zjn.studentmanagement.domain.Score;
import cn.hzxs.zjn.studentmanagement.domain.User;
import cn.hzxs.zjn.studentmanagement.sevice.ScoreService;
import cn.hzxs.zjn.studentmanagement.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author: Zjn ; Bring it on !!!
 * @Date: 2021/3/29 0029 12:40
 * Note:
 */
@RestController
public class ScoreController {
    @Autowired
    ScoreService scoreService;

    @Autowired
    UserMapper userMapper;


//    @PostMapping("/getScore")
//    public JsonResult getScoreByTime(@RequestBody Score score){
//        List<Score> scoreByTime = scoreService.getScoreByTime(score.getTIME());
//        HashMap<Object, Object> hashMap = new HashMap<>();
//        for (int i = 0; i <scoreByTime.size() ; i++) {
//            String usernameByAuthorId = userMapper.findUsernameByAuthorId
//                    (scoreByTime.get(i).getUSERID());
//            scoreByTime.get(i).setUSERNAME(usernameByAuthorId);
//            hashMap.put(i,scoreByTime.get(i));
//        }
//        return new JsonResult(hashMap);
//    }

    @PostMapping("/performance")
    public JsonResult performance(@RequestBody User user){
        int TOTALSCORE = 0;
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> performance = scoreService.performance(user.getUSERID());
        HashMap<Object, Object> history = new HashMap<>();
        for (int i = 0; i < performance.size(); i++) {
            history.put(i,performance.get(i));
            TOTALSCORE +=Integer.parseInt(performance.get(i).get("score").toString());
        }
        hashMap.put("avg",TOTALSCORE/performance.size());
        hashMap.put("history",history);
        return new JsonResult(hashMap);
    }

    @GetMapping("/teacher_planGet")    /*performancessss*/
    public JsonResult teacher_planGet(){
        int TOTALSCORE = 0;
        HashMap<Object, Object> hashMaps = new HashMap<>();
        List<Integer> userId = scoreService.getUserId();
        LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(userId);
        ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);    /*[1,2,3]*/
        for (int i = 0; i < listWithoutDuplicates.size(); i++) {
            HashMap<Object, Object> history = new HashMap<>();
            HashMap<Object, Object> hashMap = new HashMap<>();
            List<Map> performances = scoreService.performance(listWithoutDuplicates.get(i));
            for (int j = 0; j < performances.size(); j++) {
                history.put(j,performances.get(j));
                TOTALSCORE += Integer.parseInt(performances.get(j).get("score").toString());
            }
            hashMap.put("history",history);
            hashMap.put("avg",TOTALSCORE/performances.size());
            TOTALSCORE=0;
            Map<Object,Object> userInfo = scoreService.getUserInfo(listWithoutDuplicates.get(i));
            hashMap.putAll(userInfo);
            hashMaps.put(i,hashMap);
        }
        return new JsonResult(hashMaps);
    }


    @PostMapping("/submit")
    public JsonResult submit(@RequestBody Map<Object,Map<Object,Map>> map){
        HashMap<Object, Object> hashMap = new HashMap<>();
        for (int i = 0; i < map.get("data").size()-1; i++) {
            scoreService.submit(new Score(map.get("data").get(String.valueOf(i)).get("userId"),
                    map.get("data").get(String.valueOf(i)).get("score"),
                    map.get("data").get("createtime").get("time")));
        }
        hashMap.put("status","ok");
        return new JsonResult(hashMap);
    }
}
